﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussiness;
using System.IO;

public partial class BrowserUpdataOptizimation : System.Web.UI.Page
{
    private double burden = 0;
    private double temperature = 0;
    private PiLink.declare PIDeclare = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        string water = ConfigurationManager.ConnectionStrings["water"].ToString();
        string fh = ConfigurationManager.ConnectionStrings["FH"].ToString();
        if (PIDeclare == null)
            InitLink();

        PIModel model = new PIModel();
        this.lblBurden.Text =  model.GetCurrentValue(fh, PIDeclare).ToString("0.00");
        this.lblTemperature.Text =  model.GetCurrentValue(water, PIDeclare).ToString("0.00");

        if (!IsPostBack)
        {
            this.Timer1.Interval = 30000;
            BindParaType(ParaMoudle.GKXY1);
            Bind();
        }
        
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }


    protected void btnExport_Click()
    {
        

        Response.ClearContent();


        Response.Charset = "GB2312";
        Response.Flush();

        Response.Buffer = false;
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");

        Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("#1号机组运行分值日报表.xls", System.Text.Encoding.UTF8) + "");
        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();


    }

    /// <summary>
    /// 绑定参数类型数据
    /// </summary>
    /// <param name="paraMoudle"></param>
    private void BindParaType(string paraMoudle)
    {
        SysParament para = new SysParament();
        this.ddlParas.DataSource = para.GetSysParaType(paraMoudle);
        this.ddlParas.DataTextField = "PARATYPE";
        this.ddlParas.DataBind();

        this.ddlParas.Items.Insert(0, new ListItem("全部", "0"));
    }

    private void BindParaType()
    {
        SysParament para = new SysParament();
        this.ddlParas.DataSource = para.GetSysParaType();
        this.ddlParas.DataTextField = "PARATYPE";
        this.ddlParas.DataBind();

        this.ddlParas.Items.Insert(0, new ListItem("全部", "0"));
    }

    private void InitLink()
    {
        string server = ConfigurationManager.ConnectionStrings["server"].ToString();
        string piuser = ConfigurationManager.ConnectionStrings["piuser"].ToString();
        string password = ConfigurationManager.ConnectionStrings["password"].ToString();

        DB2Provider.PIBaseLink link = new DB2Provider.PIBaseLink();
        link.ServerName = server;
        link.UserName = piuser;
        link.PassWord = password;

        PIDeclare = link.GetPI();
    }
    /// <summary>
    /// 绑定最优数据
    /// </summary>
    private void Bind()
    {
        if (PIDeclare == null)
            InitLink();

        PIModel model = new PIModel();
        if (this.ddlCompute.Text == "2")
        {
            string water = ConfigurationManager.ConnectionStrings["water2"].ToString();
            string fh = ConfigurationManager.ConnectionStrings["FH2"].ToString();
            this.lblBurden.Text = model.GetCurrentValue(fh, PIDeclare).ToString("0.00");
            this.lblTemperature.Text = model.GetCurrentValue(water, PIDeclare).ToString("0.00");
        }

        burden = Convert.ToDouble(this.lblBurden.Text);
        temperature = Convert.ToDouble(this.lblTemperature.Text);
        SysParament para = new SysParament();
        DataTable dt=null;
        if (this.ddlCompute.Text == "1")
            dt = para.GetUpdataSysPara(this.ddlParas.Text, "工况寻优1");
        else
            dt = para.GetUpdataSysPara(this.ddlParas.Text, "工况寻优2");
        Optimization C_Optimization = new Optimization();
        System.Collections.ArrayList al = new ArrayList();
        foreach (DataRow dr in dt.Rows)
        {
            ViewOptimization vOp = C_Optimization.GetUpdataOptimization(burden, temperature, this.ddlCompute.Text, dr[0].ToString(), dr[2].ToString(), PIDeclare);
            vOp.ParaNameUnit = dr[1].ToString();
            al.Add(vOp);
        }

        PIDeclare.DisConnect();
        this.grvFeeInfo.DataSource = al;
        this.grvFeeInfo.DataBind();
    }
    /// <summary>
    /// 绑定参数配置数据
    /// </summary>
    /// <param name="paraType"></param>
    private void BindDll(string paraType)
    {
        SysParament para = new SysParament();
        this.ddlParas.DataSource = para.GetSysPara(paraType);
        this.ddlParas.DataTextField = "PARADESC";
        this.ddlParas.DataValueField = "PARAID";
        this.ddlParas.DataBind();
    }
    /// <summary>
    /// 查询最优工况数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    protected void grvFeeInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (this.grvFeeInfo.Rows.Count > 0)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Convert.ToDouble(e.Row.Cells[2].Text) < Convert.ToDouble(e.Row.Cells[3].Text))
                        e.Row.Cells[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Yellow;
                    else if (Convert.ToDouble(e.Row.Cells[2].Text) > Convert.ToDouble(e.Row.Cells[3].Text))
                        e.Row.Cells[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Red;

                }
                else if (e.Row.RowType == DataControlRowType.Footer)
                {
                    if (Convert.ToDouble(this.grvFeeInfo.Rows[0].Cells[2].Text) < Convert.ToDouble(this.grvFeeInfo.Rows[0].Cells[3].Text))
                        this.grvFeeInfo.Rows[0].Cells[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Yellow;
                    else if (Convert.ToDouble(this.grvFeeInfo.Rows[0].Cells[2].Text) > Convert.ToDouble(this.grvFeeInfo.Rows[0].Cells[3].Text))
                        this.grvFeeInfo.Rows[0].Cells[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
    protected void grvFeeInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvFeeInfo.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Bind();

        string water = ConfigurationManager.ConnectionStrings["water"].ToString();
        string fh = ConfigurationManager.ConnectionStrings["FH"].ToString();
        PIModel model = new PIModel();

        if (PIDeclare == null)
            InitLink();
        this.lblBurden.Text = model.GetCurrentValue(fh, PIDeclare).ToString("0.00");
        this.lblTemperature.Text = model.GetCurrentValue(water, PIDeclare).ToString("0.00");
    }

    public void ReadPara()
    {
        string sql = "SELECT * FROM ADMINISTRATOR.T_SYS_PARA";
        DB2Provider.DataLink link = new DB2Provider.DataLink();
        DataTable dt = link.ExcuteRetureTable(sql);

        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                string piname = dr[1].ToString().Replace("CSPC:01", "CSPC:02");
                string ptype = dr[4].ToString().Replace("工况寻优1", "工况寻优2");
                sql = "INSERT INTO ADMINISTRATOR.T_SYS_PARA(PARAID,PARADESC,PARATYPE,PARAMODULE,PARAUNIT,PARATAG) VALUES ('" + piname + "','" + dr[2].ToString() + "','" + dr[3].ToString() + "','" + ptype + "','" + dr[5].ToString() + "','" + piname + "')";
                link.Excute(sql);
            }
            
        }
    }
}
